package com.selfdiscipline.data.repository

import com.selfdiscipline.data.local.dao.TimeRecordDao
import com.selfdiscipline.data.local.entity.TimeRecordEntity
import com.selfdiscipline.domain.model.TimeRecord
import com.selfdiscipline.domain.repository.TimerRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.first
import java.time.LocalDateTime
import javax.inject.Inject
import kotlin.time.Duration.Companion.minutes

/**
 * 计时器仓库实现类
 * 实现计时器相关数据访问操作
 */
class TimerRepositoryImpl @Inject constructor(
    private val timeRecordDao: TimeRecordDao
) : TimerRepository {
    
    override suspend fun saveTimeRecord(
        title: String,
        category: String,
        duration: kotlin.time.Duration
    ): Long {
        val durationMinutes = duration.inWholeMinutes.toInt()
        
        val timeRecord = TimeRecordEntity(
            title = title,
            category = category,
            durationMinutes = durationMinutes,
            timestamp = LocalDateTime.now()
        )
        
        return timeRecordDao.insert(timeRecord)
    }
    
    override fun getAllTimeRecords(): Flow<List<TimeRecord>> {
        return timeRecordDao.getAllTimeRecords().map { entities ->
            entities.map { it.toDomainModel() }
        }
    }
    
    override fun getTimeRecordById(id: Long): Flow<TimeRecord?> {
        return timeRecordDao.getTimeRecordById(id).map { it.toDomainModel() }
    }
    
    override fun getTimeRecordsByCategory(category: String): Flow<List<TimeRecord>> {
        return timeRecordDao.getTimeRecordsByCategory(category).map { entities ->
            entities.map { it.toDomainModel() }
        }
    }
    
    override fun getTimeRecordsInRange(startDate: LocalDateTime, endDate: LocalDateTime): Flow<List<TimeRecord>> {
        return timeRecordDao.getTimeRecordsInRange(startDate, endDate).map { entities ->
            entities.map { it.toDomainModel() }
        }
    }
    
    override fun getTotalFocusTime(): Flow<kotlin.time.Duration> {
        return timeRecordDao.getTotalDurationMinutes().map { totalMinutes ->
            (totalMinutes ?: 0).minutes
        }
    }
    
    override fun getTotalFocusTimeInRange(startDate: LocalDateTime, endDate: LocalDateTime): Flow<kotlin.time.Duration> {
        return timeRecordDao.getTotalDurationMinutesInRange(startDate, endDate).map { totalMinutes ->
            (totalMinutes ?: 0).minutes
        }
    }
    
    override fun getCategoryDistribution(): Flow<Map<String, kotlin.time.Duration>> {
        return timeRecordDao.getCategoryDistribution().map { distribution ->
            distribution.mapValues { (_, minutes) ->
                minutes.minutes
            }
        }
    }
    
    override suspend fun deleteTimeRecord(id: Long): Boolean {
        return try {
            timeRecordDao.deleteById(id)
            true
        } catch (e: Exception) {
            false
        }
    }
    
    /**
     * 将实体映射为领域模型
     */
    private fun TimeRecordEntity.toDomainModel(): TimeRecord {
        return TimeRecord(
            id = id,
            title = title,
            category = category,
            durationMinutes = durationMinutes,
            duration = durationMinutes.minutes,
            timestamp = timestamp
        )
    }
} 